package client;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;

import server.BkCAccITF;
import server.BkLAccITF;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Date;

public class BkAddLAccGUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	
	Person ps = new Person();
	int pID = ps.getPID();
	/**
	 * Launch the application.
	 */
	
	Configuration config=new Configuration();
	String rmiAddress=config.getRmiAddress();
	BkLAccITF LAitf;
	
	
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BkAddLAccGUI frame = new BkAddLAccGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 */
	public BkAddLAccGUI() {
		setTitle("\u65B0\u5EFA\u8D37\u6B3E");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("\u8BF7\u586B\u5199\u8D37\u6B3E\u4FE1\u606F\uFF1A");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label.setBounds(39, 34, 153, 18);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("\u8D37\u6B3E\u989D\uFF1A");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_1.setBounds(81, 81, 74, 25);
		contentPane.add(label_1);
		
		textField = new JTextField();
		textField.setColumns(10);
		textField.setBounds(176, 81, 165, 28);
		contentPane.add(textField);
		
		JLabel label_2 = new JLabel("\u8D37\u6B3E\u5E74\u9650\uFF1A");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_2.setBounds(70, 135, 85, 18);
		contentPane.add(label_2);
		
		final JRadioButton radioButton = new JRadioButton("\u4E00\u5E74");
		radioButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		radioButton.setBounds(172, 130, 55, 28);
		contentPane.add(radioButton);
		
		final JRadioButton radioButton_1 = new JRadioButton("\u4E09\u5E74");
		radioButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		radioButton_1.setBounds(239, 130, 67, 28);
		contentPane.add(radioButton_1);
		
		final JRadioButton radioButton_2 = new JRadioButton("\u4E94\u5E74");
		radioButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		radioButton_2.setBounds(308, 130, 60, 28);
		contentPane.add(radioButton_2);
		
		ButtonGroup group=new ButtonGroup();
		group.add(radioButton);
		group.add(radioButton_1);
		group.add(radioButton_2);
		
		
		JButton button = new JButton("\u786E\u8BA4\u8D37\u6B3E");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					LAitf = (BkLAccITF) Naming.lookup(rmiAddress+"BankLoanAccount");
					Date startTime=new Date();
					int limit=0;
					if(radioButton.isSelected())
						limit=1;
					if(radioButton_1.isSelected())
						limit=3;
					if(radioButton_2.isSelected())
						limit=5;
					String sLASum = textField.getText();
					final double LASum=Double.parseDouble(sLASum);
					BkCAccITF CAitf = (BkCAccITF) Naming.lookup(rmiAddress+"BankCurrentAccount");
					if(LASum<=0){
						JOptionPane.showMessageDialog(null, "输入有误！");
					}else{
				        if(LAitf.addLA(pID, LASum, limit, startTime)!=-1){
							JOptionPane.showMessageDialog(null, "新增贷款成功！");
							dispose();
							BkLoanAccountGUI frame = new BkLoanAccountGUI();
							frame.setLocationRelativeTo(null);
							frame.setVisible(true);
							
				        }else{
				        	JOptionPane.showMessageDialog(null, "新增贷款失败！");
				        }
					}
					
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotBoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		
			}
		});
		button.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		button.setBounds(241, 187, 127, 49);
		contentPane.add(button);
		
		JButton button_1 = new JButton("\u67E5\u770B\u8D37\u6B3E\u5229\u7387");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "一年 ：3.00%；三 年： 4.50%；五 年 5.00%");
			}
		});
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		button_1.setBounds(202, 31, 120, 27);
		contentPane.add(button_1);
		
		JButton button_2 = new JButton("\u8FD4\u56DE");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BkLoanAccountGUI frame = new BkLoanAccountGUI();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
				dispose();
			}
		});
		button_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		button_2.setBounds(341, 10, 83, 22);
		contentPane.add(button_2);
	}
}
